package com.botann.projects.base;


import com.botann.projects.constants.Code;
import com.botann.projects.constants.Constant;
import com.botann.projects.constants.Code;
import com.botann.projects.constants.Constant;
import com.botann.projects.utils.HibernateUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Charging
 * HqlParams 处理筛选条件为null 的 忽略
 * Created by 刘山国 on 2017/12/27.
 * Copyright (c) 2017 伯坦科技. All rights reserved
 */
public class HqlParams {

    private String hql;
    private Object[] params;

    public HqlParams(String hql, Object... params) {
        this.hql = hql;
        if (params == null || params.length==0) {
            HibernateUtils.logHqlAndParameters(hql,params);
            return;
        }
        List paramsList = new ArrayList<>();
        StringBuilder hqlBuilder = new StringBuilder(hql);
        if (!hql.contains("1=1")) hqlBuilder.insert(hqlBuilder.indexOf("where")+6,"1=1 and ");
        for (int i=0;i<params.length;i++) {
            int middleIndex = hqlBuilder.indexOf("?"+i);
            if (middleIndex<0) throw new SGRuntimeException(Code.QUERY_ERROR,"查询语句错误!,没有参数位置:" + "?"+i);
            String preStr = hqlBuilder.substring(0,middleIndex);

            // 日期判断, 日期范围如果仅有一个,则补齐另一个,都没有不当做条件筛选
            if (preStr.endsWith("between ") || preStr.endsWith("between  ") || preStr.endsWith("between  ")) {
                // 如果日期有一个非null 另个一null, 则补齐
                if (params[i] != null || params[i+1] != null) {
                    if (params[i] == null)  params[i] = Constant.BEGIN_DATE;
                    if (params[i+1] == null) {
                        params[i+1] = Constant.END_DATE;
                    } else {
                        // 转成23:59s
//                        Calendar calendar = new GregorianCalendar();
//                        calendar.setTime((Date) params[i+1]);
//                        calendar.add(calendar.DATE,1);
//                        calendar.add(calendar.SECOND,-1);
//                        params[i+1] = calendar.getTime();
                    }
                }
            }
            Object param = params[i];
            // 特殊字符串置空
            if (param!=null && (param.equals("%null") || param.equals("%null") || param.equals("null%") || param.equals("null"))) param = null;

            // null 的参数条件删除, 非null按序重设位置
            String numberStr = ""+i;
            if (param == null) {
                int startIndex = Math.max(preStr.lastIndexOf(" and "),preStr.lastIndexOf(" or "));
                int endIndex = middleIndex+numberStr.length();
                if (hqlBuilder.length()>endIndex) endIndex++;
                hqlBuilder.replace(startIndex,endIndex,"");
            } else {
                if (i != paramsList.size()) hqlBuilder.replace(middleIndex+1,middleIndex+1+numberStr.length(),""+paramsList.size());
                paramsList.add(param);
            }
        }
        this.hql = hqlBuilder.toString();
        this.params = paramsList.toArray();
//        HibernateUtils.logHqlAndParameters(this.hql,this.params);
    }


    public String getHql() {
        return hql;
    }

    public Object[] getParams() {
        return params;
    }

}
